﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
using NDK.Utils;

namespace NDK.Motion.Frm
{
    public partial class Dlg_UniformVelocity : Form
    {
        private Point offset;
        private static log4net.ILog logger = null;
        private DataTable positionDataTable = null;
        private string _title;
        private string _lableText;



        public Dlg_UniformVelocity()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.Manual;
            int x = SystemInformation.PrimaryMonitorSize.Width / 2 - this.Width / 2;
            int y = SystemInformation.PrimaryMonitorSize.Height / 2 - this.Height / 2;
            this.Location = new Point(x, y);
        }

        #region 引入窗体动画效果方法
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        #endregion

        private void Dlg_UniformVelocity_Load(object sender, EventArgs e)
        {
            logger = log4net.LogManager.GetLogger(this.GetType());
            logger.Info("统一速度对话框加载成功！");


            lbl_Tip.ForeColor = Color.Red;
            lbl_Tip.Text = "注意：速度修改后将实时生效！";


            if (MachineDataManager.Instance.StationList.Count > 0)
            {
                foreach (var station in MachineDataManager.Instance.StationList)
                {
                    if (station.AxisList.Count > 0)
                    {
                        cb_Station.Items.Add(station.StationName);
                    }
                }
            }

        }



        private void lbl_Ok_MouseEnter(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Ok, true);
        }

        private void lbl_Ok_MouseLeave(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Ok, false);
        }

        private void lbl_Ok_Click(object sender, EventArgs e)
        {

            if (string.Empty == cb_Station.Text.Trim())
            {
                lbl_Tip.ForeColor = Color.Red;
                lbl_Tip.Text = "工站不能为空！";
                return;
            }
            double acc = (double)num_Acc.Value;
            double dec = (double)num_Dec.Value;
            double vel = (double)num_MoveVel.Value;

            double vel_X = (double)num_MoveVelX.Value;
            double vel_Y = (double)num_MoveVelY.Value;
            double vel_R = (double)num_MoveVelR.Value;
            double vel_Z = (double)num_MoveVelZ.Value;
            double v0 = (double)num_V0.Value;

            if (cb_Station.Text.Trim() == "ALL")
            {
                foreach (var station in MachineDataManager.Instance.StationList)
                {
                    if (station == null)
                    {
                        continue;
                    }
                    ChangeVelProfile(station.StationName, v0, vel, vel_X, vel_Y, vel_R, vel_Z, acc, dec);
                }
            }
            else
            {
                ChangeVelProfile(cb_Station.Text.Trim(), v0, vel, vel_X, vel_Y, vel_R, vel_Z, acc, dec);
            }
            this.DialogResult = DialogResult.OK;
        }
        private bool ChangeVelProfile(string stationName, double v0, double vel, double vel_X, double vel_Y, double vel_R, double vel_Z, double acc, double dec)
        {
            List<Position> posList = MachineDataManager.Instance.PositionList.FindAll(s => s.Station == stationName);
            if (posList == null || posList?.Count == 0)
            {
                return false;
            }

            foreach (var pos in posList)
            {
                if (!pos.IsAllowUniform)
                    continue;
                foreach (var velProfile in pos.VelocityProfiles)
                {
                    Axis axis = MachineDataManager.Instance.AxisList.SingleOrDefault(a => a.Name == velProfile.AxisName);
                    var axisType = axis.Name.Substring(0, 1);

                    double velTemp = 0;
                    switch (axisType)
                    {
                        case "X":
                            velTemp = vel_X;
                            break;
                        case "Y":
                            velTemp = vel_Y;
                            break;
                        case "R":
                            velTemp = vel_R;
                            break;
                        case "Z":
                            velTemp = vel_Z;
                            break;
                        default:
                            velTemp = vel;
                            break;
                    }

                    if (velTemp > axis.MaxVel)//如果设定的速度大于最大速度，就用最大速度
                    {
                        velProfile.MoveVel = axis.MaxVel;
                    }
                    else
                    {
                        velProfile.MoveVel = velTemp;
                    }
                    velProfile.Acc = acc;
                    velProfile.Dec = dec;
                    velProfile.StartVel = v0;
                }

            }
            return true;
        }

        private void lbl_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void lbl_Cancel_MouseEnter(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Cancel, true);
        }

        private void lbl_Cancel_MouseLeave(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Cancel, false);
        }

        private void num_MoveVel_ValueChanged(object sender, EventArgs e)
        {
            num_MoveVelX.Value = num_MoveVel.Value;
            num_MoveVelY.Value = num_MoveVel.Value;
            num_MoveVelZ.Value = num_MoveVel.Value;
            num_MoveVelR.Value = num_MoveVel.Value;
        }
    }
}
